package com.yhp.web.userweb;

import com.sun.org.apache.regexp.internal.RE;
import com.yhp.bo.OrderItemsCommentBO;
import com.yhp.enums.YesOrNo;
import com.yhp.pojo.OrderItems;
import com.yhp.pojo.Orders;
import com.yhp.service.userservice.CenterCommentService;
import com.yhp.utils.JSONResult;
import com.yhp.utils.PagedGridResult;
import com.yhp.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "评价商品",tags = {"评价商品的相关接口"})
@RestController
@RequestMapping("/mycomments")
public class CommentController extends BaseController {
    @Autowired
    private CenterCommentService centerCommentService;

    @PostMapping("/pending")
    @ApiOperation(value = "获得评价列表")
    public JSONResult pending(String userId,String orderId){
        //判断用户和订单是否关联
        JSONResult jsonResult = checkUserOrder(userId, orderId);
        if(jsonResult.getStatus()!= HttpStatus.OK.value()){
            return jsonResult;
        }
        //判断该笔订单是否已经评价过，评价过了就不再继续
        Orders  orders = (Orders)jsonResult.getData();
        if(orders.getIsComment()== YesOrNo.YES.type){
            return JSONResult.errorMsg("该订单已经评价");
        }

        List<OrderItems> orderItems = centerCommentService.queryPendingComment(orderId);

        return JSONResult.ok(orderItems);
    }


    @PostMapping("/saveList")
    @ApiOperation(value = "保存评论列表")
    public JSONResult saveList(String userId,
                               String orderId,
                               @RequestBody List<OrderItemsCommentBO> commentList){
        //判断用户和订单是否关联
        JSONResult jsonResult = checkUserOrder(userId, orderId);
        if(jsonResult.getStatus()!= HttpStatus.OK.value()){
            return jsonResult;
        }
        //判断评论内容不能为空
        if(commentList==null||commentList.isEmpty()){
            return JSONResult.errorMsg("评论列表不能为空");
        }

         centerCommentService.saveComment(userId, orderId, commentList);
        return JSONResult.ok();
    }


    @PostMapping("/query")
    @ApiOperation(value = "查询我的评论列表")
    public JSONResult query(String userId,Integer page,Integer pageSize){
        //用户id非空验证
        if(StringUtils.isBlank(userId)){
            return JSONResult.errorMsg("用户id不允许为空");
        }
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=COMMENT_PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = centerCommentService.queryMyComment(userId, page, pageSize);

        return JSONResult.ok(pagedGridResult);
    }
}
